package com.my.simple.web.controller.admin;

import com.my.simple.entity.auth.Role;
import com.my.simple.entity.auth.RoleOperation;
import com.my.simple.service.auth.RoleService;
import com.my.simple.web.vo.ResponseData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping(value = "role")
public class RoleController {
    private static final Logger LOGGER = LoggerFactory.getLogger(RoleController.class);
    private static final Integer USERID=10000;
    @Autowired
    private RoleService roleService;

    @PostMapping(value = "addrole")
    public ResponseEntity<ResponseData> addRole(@RequestBody Role roleItem) {
        ResponseData responseData = new ResponseData();
        roleItem.setUpdateBy(USERID);
        roleItem.setUpdateDate(new Date());
        roleService.addRole(roleItem);
        LOGGER.info("add role id is {}", roleItem.getRoleId());

        responseData.setMessage("add successful");
        ResponseEntity<ResponseData> responseEntity = new ResponseEntity<ResponseData>(responseData, HttpStatus.OK);
        return responseEntity;
    }

    @GetMapping(value = "getallrole")
    public ResponseEntity<ResponseData> getAllRole() {
        ResponseData responseData = new ResponseData();
        List<Role> roleList = roleService.getAllRole();
        responseData.setData(roleList);

        ResponseEntity<ResponseData> responseEntity = new ResponseEntity<ResponseData>(responseData, HttpStatus.OK);
        return responseEntity;
    }

    @GetMapping(value = "getrolebyid/{roleid}")
    public ResponseEntity<ResponseData> getRoleById(@PathVariable("roleid") int roleId) {
        ResponseData responseData = new ResponseData();
        Role role = roleService.getRoleById(roleId);
        responseData.setData(role);

        ResponseEntity<ResponseData> responseEntity = new ResponseEntity<ResponseData>(responseData, HttpStatus.OK);
        return responseEntity;
    }

    @DeleteMapping(value = "deleterolebyid/{roleid}")
    public ResponseEntity<ResponseData> deleteRoleById(@PathVariable("roleid") int roleId) {
        ResponseData responseData = new ResponseData();
        roleService.deleteRoleById(roleId);
        responseData.setMessage("delete successful");
        ResponseEntity<ResponseData> responseEntity = new ResponseEntity<ResponseData>(responseData, HttpStatus.OK);
        return responseEntity;
    }

    @PutMapping(value = "updaterole")
    public ResponseEntity<ResponseData> updateRole(@RequestBody Role roleItem) {
        ResponseData responseData = new ResponseData();
        Integer roleId = roleItem.getRoleId();
        Role findRole = roleService.getRoleById(roleId);

        findRole.setRoleName(roleItem.getRoleName());
        findRole.setDescription(roleItem.getDescription());
        findRole.setActive(roleItem.getActive());
        findRole.setUpdateDate(new Date());
        findRole.setUpdateBy(USERID);
        roleService.updateRole(findRole);

        responseData.setMessage("update role success");
        ResponseEntity<ResponseData> responseEntity = new ResponseEntity<ResponseData>(responseData, HttpStatus.OK);
        return responseEntity;
    }

    @PutMapping(value = "updateroleoperation")
    public ResponseEntity<ResponseData> updateRoleOperation(@RequestBody List<RoleOperation> roleOperationEntityList) {
        ResponseData responseData = new ResponseData();
        roleService.updateRoleOperation(roleOperationEntityList);
        responseData.setMessage("add successful");
        ResponseEntity<ResponseData> responseEntity = new ResponseEntity<ResponseData>(responseData, HttpStatus.OK);
        return responseEntity;
    }

    @GetMapping(value = "getroleoperationbyroleid/{roleid}")
    public ResponseEntity<ResponseData> getRoleOperationByRoleId(@PathVariable("roleid") int roleId) {
        ResponseData responseData = new ResponseData();
        List<RoleOperation> roleOperationEntityList = roleService.getRoleOperationByRoleId(roleId);
        responseData.setData(roleOperationEntityList);
        ResponseEntity<ResponseData> responseEntity = new ResponseEntity<ResponseData>(responseData, HttpStatus.OK);
        return responseEntity;
    }

}
